package gtgold;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.text.DecimalFormat;
import javax.swing.*;

/**
 * Dialog for displaying inventory
 * and other options
 * 
 * @author jen, jo
 */
public class InventoryMenu extends JDialog
{
	private static final long serialVersionUID = 1L;
	private final GameManager manager;
	private JButton cancelButton;
	private DecimalFormat twoPlaces = new DecimalFormat("0.00");
	private ArrayList<JLabel> itemLabels;

	public InventoryMenu(final GameManager manager)
	{
		super(OregonTrail.frame, "Other Options", true);
		this.manager = manager;

		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setResizable(false);
		setPreferredSize(OregonTrail.VGA);
		setLayout(new GridLayout(1, 1));

		// Main Bg JPanel
		bgImagePanel mainPanel = new bgImagePanel(
				"src/gtgold/img/inventoryBg.jpg");
		add(mainPanel, BorderLayout.CENTER);
		mainPanel.setLayout(new GridLayout(1, 1));

		// Attributes JPanel
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(15, 5));
		for (int i = 0; i < 7; i++)
		{
			panel.add(new JLabel());
		}
		panel.add(new JLabel("<html><B>Party Health</B></html>"));
		panel.add(new JLabel("<html><B>Inventory</B></html>"));
		panel.add(new JLabel());

		List<Person> party = manager.data.party;

		itemLabels = new ArrayList<JLabel>();

		for (int i = 0; i < 8; i++)
		{
			panel.add(new JLabel());
			panel.add(new JLabel());
			if (i < party.size())
			{
				Person p = party.get(i);
				String color = (p.health() < Person.OK) ? "red" : "black";				
				String status = "<html>" + p.name + " - <font color=" + color + ">"
						+ p.status() + "</font></html>";

				panel.add(new JLabel(status));
			} else
			{
				panel.add(new JLabel());
			}

			itemLabels.add(new JLabel());
			panel.add(itemLabels.get(i));

			panel.add(new JLabel());
		}

		updateItemLabels();

		for (int i = 0; i < 21; i++)
		{
			panel.add(new JLabel());
		}

		JButton dumpButton = new JButton("Dump Junk");
		dumpButton.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent e)
			{
				manager.openDropMenu();
				updateItemLabels();
			}

		});
		panel.add(dumpButton);

		// Add pace selection menu
		{
			JButton paceButton = new JButton("Change pace");
			paceButton.addActionListener(new ActionListener()
			{
				private Map<Pace, String> buttons;

				{
					buttons = new EnumMap<Pace, String>(Pace.class);
					for (Pace p : Pace.values())
					{
						if (p != Pace.STOPPED)
						{
							buttons.put(p, p.toString());
						}
					}
					buttons = Collections.unmodifiableMap(buttons);
				}

				public void actionPerformed(ActionEvent ae)
				{
					Pace response = manager.openOptionDialog("Select a pace",
							buttons, true);
					if (response != null)
					{
						gameData().pace = response;
					}
				}
			});
			panel.add(paceButton);
		}

		// Add ration selection menu
		{
			JButton rationButton = new JButton("Change ration");
			rationButton.addActionListener(new ActionListener()
			{
				private Map<Ration, String> buttons;

				{
					buttons = new EnumMap<Ration, String>(Ration.class);
					for (Ration p : Ration.values())
					{
						if (p != Ration.NONE)
						{
							buttons.put(p, p.toString());
						}
					}
					buttons = Collections.unmodifiableMap(buttons);
				}

				public void actionPerformed(ActionEvent ae)
				{
					Ration response = manager.openOptionDialog(
							"Select a ration", buttons, true);
					if (response != null)
					{
						gameData().ration = response;
					}
				}
			});
			panel.add(rationButton);
		}

		cancelButton = new JButton("Done");
		cancelButton.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				dispose(); // execution resumes in whatever opened the dialog
			}
		});
		// Adding JPanels together
		panel.add(cancelButton);
		panel.setOpaque(false);
		mainPanel.add(panel);
		add(mainPanel);

		pack();
		setLocationRelativeTo(OregonTrail.frame);
		setVisible(true);
	}

	private void updateItemLabels()
	{
		for (int i = 0; i < 8; i++)
		{
			if (i == 0)
			{
				itemLabels.get(i).setText(
						"Money: $"
								+ twoPlaces.format(gameData().inventory
										.get(Item.MONEY)));
			} else if (i == 1)
			{
				itemLabels.get(i).setText(
						"Oxen: " + (int) gameData().inventory.get(Item.OXEN));
			} else if (i == 2)
			{
				itemLabels.get(i).setText(
						"Food: " + (int) gameData().inventory.get(Item.FOOD)
								+ " lbs");
			} else if (i == 3)
			{
				itemLabels.get(i).setText(
						"Clothes: "
								+ (int) gameData().inventory.get(Item.CLOTHES)
								+ " sets");
			} else if (i == 4)
			{
				itemLabels.get(i).setText(
						"Bullets: "
								+ (int) gameData().inventory.get(Item.BULLETS)
								+ " boxes");
			} else if (i == 5)
			{
				itemLabels.get(i).setText(
						"Wheels: "
								+ (int) gameData().inventory.get(Item.WHEELS));
			} else if (i == 6)
			{
				itemLabels.get(i).setText(
						"Axles: " + (int) gameData().inventory.get(Item.AXLES));
			} else if (i == 7)
			{
				itemLabels.get(i).setText(
						"Tounges: "
								+ (int) gameData().inventory.get(Item.TOUNGES));
			}

		}
	}

	private GameData gameData()
	{
		return manager.data;
	}

}
